package me.firstapp.api.shop;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.shop.Category;
import me.firstapp.service.shop.CategoryService;

@Controller
@RequestMapping(value = "/category")
@Api(value = "/category", description = "商品分类相关接口")
public class CategoryApi extends BaseApi {

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@RequestMapping(value = "/findCategories.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询商品分类", notes = "查询商品分类")
	public void findCategories(HttpServletRequest request, HttpServletResponse response) {
		ListObject<Category> result = new ListObject<Category>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			@SuppressWarnings("unchecked")
			List<Category> categories = (List<Category>) redisTemplate.opsForValue().get("categoriesCache"+apiClientId);
			if (categories == null || categories.isEmpty()) {
				categories = categoryService.findCategories(apiClientId);
			}
			result.setItems(categories);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findCategoryPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询品类", notes = "分页查询品类")
	public void findCategoryPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Category> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Category> categoryPage = categoryService.findSectionPage(pageNo, pageSize, searchParams, sortParam,
					sortType, apiClientId);
			JsonPage<Category> jsonPage = new JsonPage<Category>();
			jsonPage.setList(categoryPage.getEntities());
			jsonPage.setPageNo(categoryPage.getPageNo());
			jsonPage.setPageSize(categoryPage.getPageSize());
			jsonPage.setTotalCount(categoryPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findCategoryById.do", method = RequestMethod.GET)
	@ApiOperation(value = "通过id查询品类", notes = "通过id查询品类")
	public void findCategoryById(Long categoryId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Category> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Category category = categoryService.findCategoryById(categoryId, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(category);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "addCategory.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增品类", notes = "新增品类")
	public void addCategory(String name, String description, Integer sort, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Category> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Category category = categoryService.addCategory(name, description, sort, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(category);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateCategory.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新品类", notes = "更新品类")
	public void updateCategory(Long categoryId, String name, String description, Integer sort,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Category> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Category category = categoryService.updateCategory(categoryId, name, description, sort, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(category);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteCategorys.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除品类", notes = "删除品类")
	public void deleteCategorys(String categoryIds, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			categoryService.deleteCategorys(categoryIds, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateCategoryCache.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新商品种类缓存", notes = "更新商品种类缓存")
	public void updateCategoryCache(HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<Category> categories = categoryService.findCategories(apiClientId);
			redisTemplate.opsForValue().set("categoriesCache"+apiClientId, categories);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
